package com.hvilela.filters;

/**
 * @author Henrique de Souza Vilela
 * henrique.vilela@gmail.com
 */
import java.awt.Color;
import java.awt.image.RGBImageFilter;

public class SepiaFilter extends RGBImageFilter {
	int depth;

	public SepiaFilter(int depth) {
		if (depth < 0) {
			this.depth = 0;
		}

		this.depth = depth;
		canFilterIndexColorModel = true;
	}

	public SepiaFilter() {
		this(25);
	}

	@Override
	public int filterRGB(int x, int y, int rgb) {
		int r, g, b, gray;

		Color c = new Color(rgb);
		r = c.getRed();
		g = c.getGreen();
		b = c.getBlue();

		gray = (int) Math.round(r * 0.3 + g * 0.59 + b * 0.11);
		
		r = g = b = gray;

		r = r + (depth * 2);
		g = g + depth;
		if (r > 255) {
			r = 255;
		}
		if (g > 255) {
			g = 255;
		}
		c = new Color(r, g, b);
		return c.getRGB();
	}
}